1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkElementIndex;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkPositionIndexes;
23
24 import com.google.common.annotations.Beta;
25 import com.google.common.annotations.GwtCompatible;
26 import com.google.common.base.Converter;
27
28 import java.io.Serializable;
29 import java.util.AbstractList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34 import java.util.RandomAccess;
35
36
37
38
39
40
41
42
43
44
45
46
47 @GwtCompatible(emulated = true)
48 public final class Shorts {
49 private Shorts() {}
50
51
52
53
54
55 public static final int BYTES = Short.SIZE / Byte.SIZE;
56
57
58
59
60
61
62 public static final short MAX_POWER_OF_TWO = 1 << (Short.SIZE - 2);
63
64
65
66
67
68
69
70
71 public static int hashCode(short value) {
72 return value;
73 }
74
75
76
77
78
79
80
81
82
83
84 public static short checkedCast(long value) {
85 short result = (short) value;
86 if (result != value) {
87
88 throw new IllegalArgumentException("Out of range: " + value);
89 }
90 return result;
91 }
92
93
94
95
96
97
98
99
100
101 public static short saturatedCast(long value) {
102 if (value > Short.MAX_VALUE) {
103 return Short.MAX_VALUE;
104 }
105 if (value < Short.MIN_VALUE) {
106 return Short.MIN_VALUE;
107 }
108 return (short) value;
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public static int compare(short a, short b) {
124 return a - b;
125 }
126
127
128
129
130
131
132
133
134
135
136 public static boolean contains(short[] array, short target) {
137 for (short value : array) {
138 if (value == target) {
139 return true;
140 }
141 }
142 return false;
143 }
144
145
146
147
148
149
150
151
152
153
154 public static int indexOf(short[] array, short target) {
155 return indexOf(array, target, 0, array.length);
156 }
157
158
159 private static int indexOf(
160 short[] array, short target, int start, int end) {
161 for (int i = start; i < end; i++) {
162 if (array[i] == target) {
163 return i;
164 }
165 }
166 return -1;
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180 public static int indexOf(short[] array, short[] target) {
181 checkNotNull(array, "array");
182 checkNotNull(target, "target");
183 if (target.length == 0) {
184 return 0;
185 }
186
187 outer:
188 for (int i = 0; i < array.length - target.length + 1; i++) {
189 for (int j = 0; j < target.length; j++) {
190 if (array[i + j] != target[j]) {
191 continue outer;
192 }
193 }
194 return i;
195 }
196 return -1;
197 }
198
199
200
201
202
203
204
205
206
207
208 public static int lastIndexOf(short[] array, short target) {
209 return lastIndexOf(array, target, 0, array.length);
210 }
211
212
213 private static int lastIndexOf(
214 short[] array, short target, int start, int end) {
215 for (int i = end - 1; i >= start; i--) {
216 if (array[i] == target) {
217 return i;
218 }
219 }
220 return -1;
221 }
222
223
224
225
226
227
228
229
230
231 public static short min(short... array) {
232 checkArgument(array.length > 0);
233 short min = array[0];
234 for (int i = 1; i < array.length; i++) {
235 if (array[i] < min) {
236 min = array[i];
237 }
238 }
239 return min;
240 }
241
242
243
244
245
246
247
248
249
250 public static short max(short... array) {
251 checkArgument(array.length > 0);
252 short max = array[0];
253 for (int i = 1; i < array.length; i++) {
254 if (array[i] > max) {
255 max = array[i];
256 }
257 }
258 return max;
259 }
260
261
262
263
264
265
266
267
268
269
270 public static short[] concat(short[]... arrays) {
271 int length = 0;
272 for (short[] array : arrays) {
273 length += array.length;
274 }
275 short[] result = new short[length];
276 int pos = 0;
277 for (short[] array : arrays) {
278 System.arraycopy(array, 0, result, pos, array.length);
279 pos += array.length;
280 }
281 return result;
282 }
283
284 private static final class ShortConverter
285 extends Converter<String, Short> implements Serializable {
286 static final ShortConverter INSTANCE = new ShortConverter();
287
288 @Override
289 protected Short doForward(String value) {
290 return Short.decode(value);
291 }
292
293 @Override
294 protected String doBackward(Short value) {
295 return value.toString();
296 }
297
298 @Override
299 public String toString() {
300 return "Shorts.stringConverter()";
301 }
302
303 private Object readResolve() {
304 return INSTANCE;
305 }
306 private static final long serialVersionUID = 1;
307 }
308
309
310
311
312
313
314
315 @Beta
316 public static Converter<String, Short> stringConverter() {
317 return ShortConverter.INSTANCE;
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 public static short[] ensureCapacity(
337 short[] array, int minLength, int padding) {
338 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
339 checkArgument(padding >= 0, "Invalid padding: %s", padding);
340 return (array.length < minLength)
341 ? copyOf(array, minLength + padding)
342 : array;
343 }
344
345
346 private static short[] copyOf(short[] original, int length) {
347 short[] copy = new short[length];
348 System.arraycopy(original, 0, copy, 0, Math.min(original.length, length));
349 return copy;
350 }
351
352
353
354
355
356
357
358
359
360
361 public static String join(String separator, short... array) {
362 checkNotNull(separator);
363 if (array.length == 0) {
364 return "";
365 }
366
367
368 StringBuilder builder = new StringBuilder(array.length * 6);
369 builder.append(array[0]);
370 for (int i = 1; i < array.length; i++) {
371 builder.append(separator).append(array[i]);
372 }
373 return builder.toString();
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392 public static Comparator<short[]> lexicographicalComparator() {
393 return LexicographicalComparator.INSTANCE;
394 }
395
396 private enum LexicographicalComparator implements Comparator<short[]> {
397 INSTANCE;
398
399 @Override
400 public int compare(short[] left, short[] right) {
401 int minLength = Math.min(left.length, right.length);
402 for (int i = 0; i < minLength; i++) {
403 int result = Shorts.compare(left[i], right[i]);
404 if (result != 0) {
405 return result;
406 }
407 }
408 return left.length - right.length;
409 }
410 }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 public static short[] toArray(Collection<? extends Number> collection) {
428 if (collection instanceof ShortArrayAsList) {
429 return ((ShortArrayAsList) collection).toShortArray();
430 }
431
432 Object[] boxedArray = collection.toArray();
433 int len = boxedArray.length;
434 short[] array = new short[len];
435 for (int i = 0; i < len; i++) {
436
437 array[i] = ((Number) checkNotNull(boxedArray[i])).shortValue();
438 }
439 return array;
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456 public static List<Short> asList(short... backingArray) {
457 if (backingArray.length == 0) {
458 return Collections.emptyList();
459 }
460 return new ShortArrayAsList(backingArray);
461 }
462
463 @GwtCompatible
464 private static class ShortArrayAsList extends AbstractList<Short>
465 implements RandomAccess, Serializable {
466 final short[] array;
467 final int start;
468 final int end;
469
470 ShortArrayAsList(short[] array) {
471 this(array, 0, array.length);
472 }
473
474 ShortArrayAsList(short[] array, int start, int end) {
475 this.array = array;
476 this.start = start;
477 this.end = end;
478 }
479
480 @Override public int size() {
481 return end - start;
482 }
483
484 @Override public boolean isEmpty() {
485 return false;
486 }
487
488 @Override public Short get(int index) {
489 checkElementIndex(index, size());
490 return array[start + index];
491 }
492
493 @Override public boolean contains(Object target) {
494
495 return (target instanceof Short)
496 && Shorts.indexOf(array, (Short) target, start, end) != -1;
497 }
498
499 @Override public int indexOf(Object target) {
500
501 if (target instanceof Short) {
502 int i = Shorts.indexOf(array, (Short) target, start, end);
503 if (i >= 0) {
504 return i - start;
505 }
506 }
507 return -1;
508 }
509
510 @Override public int lastIndexOf(Object target) {
511
512 if (target instanceof Short) {
513 int i = Shorts.lastIndexOf(array, (Short) target, start, end);
514 if (i >= 0) {
515 return i - start;
516 }
517 }
518 return -1;
519 }
520
521 @Override public Short set(int index, Short element) {
522 checkElementIndex(index, size());
523 short oldValue = array[start + index];
524
525 array[start + index] = checkNotNull(element);
526 return oldValue;
527 }
528
529 @Override public List<Short> subList(int fromIndex, int toIndex) {
530 int size = size();
531 checkPositionIndexes(fromIndex, toIndex, size);
532 if (fromIndex == toIndex) {
533 return Collections.emptyList();
534 }
535 return new ShortArrayAsList(array, start + fromIndex, start + toIndex);
536 }
537
538 @Override public boolean equals(Object object) {
539 if (object == this) {
540 return true;
541 }
542 if (object instanceof ShortArrayAsList) {
543 ShortArrayAsList that = (ShortArrayAsList) object;
544 int size = size();
545 if (that.size() != size) {
546 return false;
547 }
548 for (int i = 0; i < size; i++) {
549 if (array[start + i] != that.array[that.start + i]) {
550 return false;
551 }
552 }
553 return true;
554 }
555 return super.equals(object);
556 }
557
558 @Override public int hashCode() {
559 int result = 1;
560 for (int i = start; i < end; i++) {
561 result = 31 * result + Shorts.hashCode(array[i]);
562 }
563 return result;
564 }
565
566 @Override public String toString() {
567 StringBuilder builder = new StringBuilder(size() * 6);
568 builder.append('[').append(array[start]);
569 for (int i = start + 1; i < end; i++) {
570 builder.append(", ").append(array[i]);
571 }
572 return builder.append(']').toString();
573 }
574
575 short[] toShortArray() {
576
577 int size = size();
578 short[] result = new short[size];
579 System.arraycopy(array, start, result, 0, size);
580 return result;
581 }
582
583 private static final long serialVersionUID = 0;
584 }
585 }